package com.example.test.comutil;

import android.os.Build;
import android.view.View;

import androidx.annotation.RequiresApi;

import java.util.Calendar;

/**
 * 防抖动
 */
public class CheckDoubleClickUtils {
    private static final String TAG = "timec";
    // 两次点击间隔不能少于1000ms
    private static final int FAST_CLICK_DELAY_TIME = 1000;
    private static long timeLast = 0;
    private static int timeInterval = 100;

    /**
     * 判断当前点击间隔的时间是否大于1秒
     * @param view 控件对象
     * @return true 为间隔的时间是大于1秒， false 为间隔的时间是少于1秒
     */
    @RequiresApi(api = Build.VERSION_CODES.DONUT)
    public static boolean isDoubleClick(View view){
        Object tag = view.getTag(view.getId());
        long beforeTimemiles = tag != null ? (long) tag : 0;
        long timeInMillis = Calendar.getInstance().getTimeInMillis();
        view.setTag(view.getId(),timeInMillis);
        return timeInMillis - beforeTimemiles > FAST_CLICK_DELAY_TIME;
    }

    /**
     * 判断当前点击间隔的时间
     * @param view 控件对象
     * @param jittertime 间隔时间
     * @return true 为间隔的时间是大于传入的间隔时间， false 为间隔的时间是少于传入的间隔时间
     */
    @RequiresApi(api = Build.VERSION_CODES.DONUT)
    public static boolean isDoubleClick(View view, long jittertime){
        Object tag = view.getTag(view.getId());
        long beforeTimemiles = tag != null ? (long) tag : 0;
        long timeInMillis = Calendar.getInstance().getTimeInMillis();
        view.setTag(view.getId(),timeInMillis);
        return timeInMillis - beforeTimemiles > jittertime;
    }

    /**
     * 判断当前点击间隔的时间
     *
     * @param timeInterval 间隔时间
     * @return 为间隔的时间是大于传入的间隔时间， false 为间隔的时间是少于传入的间隔时间
     */
    public static boolean isDoubleClick(int timeInterval) {
        boolean flag = true;
        long timeNext = System.currentTimeMillis();
        if (timeNext - timeLast >timeInterval) {
            flag = false;
        }
        timeLast = timeNext;
        return flag;
    }
}
